#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// 写点名日志的函数
int write_log(const char *soft_install_path, const char *choose_name){
    // 获取日期用来生成日志文件名
    char log_file_pathname[256] = {0};
    time_t sec = 0;
    time(&sec);
    struct tm *p_time = localtime(&sec);
    sprintf(log_file_pathname, "%s/log/name_file-%04d%02d%02d.log", \
        soft_install_path, p_time->tm_year+1900, p_time->tm_mon+1, p_time->tm_mday);
    // 打开日志文件
    FILE *fp_log = fopen(log_file_pathname, "a");
    if(NULL == fp_log){
        printf("打开日志文件失败\n");
        exit(EXIT_FAILURE);
    }
    char log_buff[256] = {0};
    sprintf(log_buff, "name_find_log : %04d年%02d月%02d日 - %02d:%02d:%02d  [%s] 同学回答了问题\n", \
                p_time->tm_year+1900, p_time->tm_mon+1, p_time->tm_mday,\
                p_time->tm_hour, p_time->tm_min, p_time->tm_sec, choose_name);
    // 写日志
    fwrite(log_buff, 1, strlen(log_buff), fp_log);
    fclose(fp_log);
}

// 写点到谁日志的函数
int write_where_log(const char *soft_install_path, const char *cur_name_list_file, const char *new_name){
    char pathname[256] = {0};
    sprintf(pathname, "%s/log/sequence_access_position", soft_install_path);
    FILE *fp = NULL;
    if(NULL == (fp = fopen(pathname, "w"))){ // w 清空原有内容
        printf("日志文件打开失败\n");
        exit(EXIT_FAILURE);
    }
    fwrite(cur_name_list_file, 1, strlen(cur_name_list_file), fp);
    fputc('\n', fp); // 换行符
    fwrite(new_name, 1, strlen(new_name), fp);
    fclose(fp);
    return 0;
}